package user_jurisdiction;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;

import DB.DB_connection_pool;

public class User_jurisdiction_servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		doPost(request,response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		JSONObject jsonObj = new JSONObject();
		
		try{
		if(session.getAttribute("user_account")!=null){
		String user_account = session.getAttribute("user_account").toString();      //获取账号
		String nickname = "";
		String user_photo_path = "";
		
		String select_user_data = "select * from user_data where user_account = '"+user_account+"'";
		Connection conn = DB_connection_pool.data_pool.getConnection();
		PreparedStatement ps_select= conn.prepareStatement(select_user_data);
		ResultSet rs = ps_select.executeQuery();
		
		if(rs.next()){
			nickname = rs.getString("user_nickname");                  //用户昵称
			user_photo_path = rs.getString("user_photo_path");             //用户头像URL
		}
		
		rs.close();
		ps_select.close();
		conn.close();
		//String nickname = session.getAttribute("nickname").toString();            //获取昵称
		//String user_photo_path = session.getAttribute("user_photo_path").toString();     //获取头像url
	
		//System.out.print(user_account+"   "+nickname+"    "+user_photo_path);
		
		if(!user_account.equals("")){
			jsonObj.put("nickname", nickname);
			jsonObj.put("user_account", user_account);
			jsonObj.put("user_photo_path", user_photo_path);
			jsonObj.put("result", true);
			
		//out.print("{\"nickname\":\""+nickname+"\",\"user_account\":\""+user_account+"\",\"user_photo_path\":\""+user_photo_path+"\",\"result\":true}");    //已经登陆
		}
		else{
			jsonObj.put("result", false);     //还未登陆
		}
		}else{
			jsonObj.put("result", false);     //还未登陆
		}
		}
		catch(Exception e){
			jsonObj.put("result", false);    //还未登陆
			e.printStackTrace();
		}
		
        out.print(jsonObj.toString());
		out.close();
		
	}

}
